package com.neusoft.nep.NEPG.controller;

import com.neusoft.nep.entity.Gridder;
import com.neusoft.nep.entity.nepsFeedback;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import static com.neusoft.tools.AlertUtils.showDialog;
import static com.neusoft.tools.FileUtils.*;
import static com.neusoft.tools.FileUtils.writeListObject;

public class LoginController implements Initializable {
    @FXML
    private TextField phoneNumber;
    @FXML
    private PasswordField password;
    @FXML
    private Button login;

    @FXML
    public void login() throws IOException {
        //读取用户名和密码
        String phone = phoneNumber.getText();
        String pass = password.getText();
        //检查是否正确
        Map<String, Gridder> userMap = readMapObject("gridder.txt");
        if (!userMap.containsKey(phone)) showDialog("警告！","用户名不存在！");
        else if (!userMap.get(phone).getPassword().equals(pass)) showDialog("警告！","密码错误！");
        else{
            //登录成功
            Stage stage = (Stage) login.getScene().getWindow();
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/FeedbackListView.fxml"));
            Parent root = fxmlLoader.load();
            Scene scene = new Scene(root);
            stage.setScene(scene);
            FeedbackListViewController controller = fxmlLoader.getController();
            //传递当前用户信息
            controller.initGridder(userMap.get(phone));
        }
    }

    public void initialize (URL location, ResourceBundle resources) {

        //初始化用户名和密码
        Map<String, Gridder> gridderMap = readMapObject("gridder.txt");
        Gridder gridder1 = new Gridder("123456", "张三", "1452", "辽宁", "沈阳市");
        Gridder gridder2 = new Gridder("123456", "赵四", "147", "吉林", "<UNK>");
        Gridder gridder3 = new Gridder("123456", "赵六", "1477", "北京", "<UNK>");
        Gridder gridder4 = new Gridder("123456", "诸葛亮", "742", "上海", "<UNK>");
        Gridder gridder5 = new Gridder("123456", "王五", "785725", "黑龙江", "<UNK>");

        gridderMap.put("1452", gridder1);
        gridderMap.put("147", gridder2);
        gridderMap.put("785725", gridder5);
        gridderMap.put("1477", gridder3);
        gridderMap.put("742", gridder4);

        try {
            writeMapObject("gridder.txt", gridderMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //初始化反馈信息
        String filename = "NEPGworks.txt";
        Map<String, List<nepsFeedback>> nepgFeedbackMap = readMapObject(filename);
        List<nepsFeedback> feedbackList = nepgFeedbackMap.getOrDefault(gridder1.getIdAsString(), new ArrayList<>());
//        feedbackList.add(new nepsFeedback(1,
//                "张三", "12345678901", "江苏省", "南京市", "玄武区xx路1号", "中度", "异味明显", "2025-06-18 10:00:00"
//        ));
//        feedbackList.add(new nepsFeedback(2,
//                "李四", "12345678901", "江苏省", "南京市", "秦淮区xx巷2号", "轻度", "水体有点浑浊", "2025-06-18 11:00:00"
//        ));
//        feedbackList.add(new nepsFeedback(3,
//                "王五", "12345678901", "江苏省", "南京市", "鼓楼区xx街3号", "严重", "黑色液体排放", "2025-06-18 12:00:00"
//        ));
        nepgFeedbackMap.put(gridder1.getIdAsString(), feedbackList);
        try {
            writeMapObject(filename, nepgFeedbackMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
